﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using compshop.Modules.GeneralProductParameters.Presenter;
using compshop.Infrastructure.CompShopDataService;

namespace compshop.Modules.GeneralProductParameters.View
{
    /// <summary>
    /// Interaction logic for GeneralProductParametersView.xaml
    /// </summary>
    public partial class GeneralProductParametersView : UserControl, IViewGeneralProductParameters
    {
        public GeneralProductParametersView()
        {
            InitializeComponent();
        }

        public GeneralProductParametersView(GeneralProductParametersPresenter presenter) 
            : this()
        {
            Presenter = presenter;
            Presenter.View = this;
        }

        public GeneralProductParametersPresenter Presenter
        {
            get { return DataContext as GeneralProductParametersPresenter; }
            set
            {
                DataContext = value;
                stackPanel.Children.Clear();
                int counter = 0;
                TextBlock txtbl;

                if (Presenter.SelectedGeneralProduct != null)
                {
                    txtbl = new TextBlock();
                    txtbl.TextAlignment = TextAlignment.Center;
                    txtbl.Background = Brushes.Chocolate;
                    txtbl.FontSize = 14;
                    txtbl.Margin = new Thickness(2, 2, 2, 2);
                    txtbl.Text = Presenter.SelectedGeneralProduct.SubCategory.Name + ": " + 
                        Presenter.SelectedGeneralProduct.Producer.Name + " " + 
                        Presenter.SelectedGeneralProduct.Name;
                    stackPanel.Children.Add(txtbl);
                }

                foreach (GeneralProductParameter param in Presenter.Parameters)
                {
                    txtbl = new TextBlock();
                    txtbl.TextAlignment = TextAlignment.Left;
                    txtbl.HorizontalAlignment = HorizontalAlignment.Stretch;
                    txtbl.Text = param.Parameter.Name + " : " + param.Value.ToString() + " " + param.Parameter.Unit;
                    txtbl.Margin = new Thickness(2, 2, 2, 2);
                    if (counter%2 != 0 )
                    {
                        txtbl.Background = Brushes.LightBlue;
                    }
                                       
                    stackPanel.Children.Add(txtbl);
                    counter++;
                }
            }
        }

        public void Clear()
        {
            stackPanel.Children.Clear();
        }

    }
}
